﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BiocityFilmquizAdmin.Src.DatabaseAccess;

namespace BiocityFilmquizAdmin
{
    public partial class Users : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void Page_PreRender(object sender, System.EventArgs e)
        {
            renderLoggedInBoxes();
        }

        private void renderLoggedInBoxes()
        {
            if (Session["loggedIn"] != null && (bool)Session["loggedIn"])
            {
                loggedInDiv.Visible = true;
                notLoggedInDiv.Visible = false;

                doIfLoggedIn();
            }
            else
            {
                loggedInDiv.Visible = false;
                notLoggedInDiv.Visible = true;
            }
        }

        private void doIfLoggedIn()
        {
            NumberOfUsersLabel.Text = AdminHelper.getInstance().getAllUsers().Count.ToString();


        }

        protected void btnExportCSV_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
             "attachment;filename=GridViewExport.csv");

            Response.Charset = "";
            Response.ContentType = "application/text";

            usersView.AllowPaging = false;
            usersView.DataBind();
        
            StringBuilder sb = new StringBuilder();

            for (int k = 0; k < 4; k++)
            {
                sb.Append(usersView.Columns[k].HeaderText + ';');
            }
            sb.Append("\r\n");
            
            for (int i = 0; i < usersView.Rows.Count; i++)
            {
                for (int k = 0; k < 4; k++)
                {
                    sb.Append(usersView.Rows[i].Cells[k].Text + ';');
                }
                sb.Append("\r\n");
            }
            Response.Output.Write(sb.ToString());
            Response.Flush();
            Response.End();
        }


        protected void deleteUserButton_Click(object sender, EventArgs e)
        {
            AdminHelper.getInstance().deleteUserEntirely(Int32.Parse(deleteUserText.Text));
        }
    }


}